#!/bin/bash

#Define name of test files
code_format="code_file"

#Define name of files with expected output
result_format="result_file"

#Define name of files used to store output of compiled test files
temp_format="test_file"

#Define extension of all files used
extension=".txt"

#Define name of folder where test files and expected output are stored
folder="test_folder"

#Define name of file with compiled code
intermediate="intermediate_code"

#Define name of compiler
program="./programa"

#Find number of tests to do
number_of_files=$(ls $folder/$code_format* | wc -l)

echo "Launching $number_of_files automated tests"

#Compile the compiler
make > /dev/null

for i in $(seq 1 $number_of_files)
do
	#Print first line of current code_file without the first 2 characters(//)
	echo -ne "Test$i:\t$(head -n 1 $folder/$code_format$i$extension | cut -c3-)\t\t"
	
	#Compile code_file silently
	$program < $folder/$code_format$i$extension > /dev/null
	ilasm $intermediate.il > /dev/null
	
	#Execute compiled code_file and redirects ouput to temp_file
	mono $intermediate.exe > $folder/$temp_format$i$extension
	
	#Compare actual output with expected output
	if diff $folder/$temp_format$i$extension $folder/$result_format$i$extension > /dev/null ; then
		echo -e "\e[00;32mPassed\e[00m"
	else
		echo -e "\e[0;31mFailed\e[00m"
		echo "Current output:"
		cat $folder/$temp_format$i$extension
		echo "Expected output:"
		cat $folder/$result_format$i$extension
	fi
	
	#Remove temp_file
	rm $folder/$temp_format$i$extension
done
make clean > /dev/null
echo "Automated tests completed"

#Creado por Frederic Cartallier
